/*
 * @Description  : 用户表单组件
 * @Author       : Keyman
 * @Date         : 2025-11-26 22:13:52
 * @LastEditors  : Keyman
 * @LastEditTime : 2025-11-28 17:23:24
 */

import {
  Col,
  Form,
  Input,
  message,
  Modal,
  Radio,
  Row,
  Select,
  type ModalProps,
} from "antd";
import { useEffect } from "react";
import { useSelector } from "react-redux";
import { editUser } from "../../api/userList";

const UserForm: React.FC<ModalProps> = (props) => {
  const { open, onCancel, title } = props;
  console.log(props);
  const [form] = Form.useForm();
  const { userData } = useSelector((state: any) => state.userSlice);

  useEffect(() => {
    if (title === "新增租户") {
      form.resetFields();
    } else {
      form.setFieldsValue(userData);
    }
  }, [open]);

  const handleOK = () => {
    form
      .validateFields()
      .then(async (values) => {
        const { data } = await editUser(values);
        message.success(data);
        onCancel?.({} as any);
      })
      .catch((err) => {
        console.log(err);
      });
  };

  return (
    <>
      <Modal
        title={title}
        open={open}
        onCancel={onCancel}
        width={800}
        onOk={handleOK}
      >
        <Form form={form}>
          <Row gutter={16}>
            <Col span={12}>
              <Form.Item
                label="客户名称"
                name="name"
                rules={[{ required: true }, { message: "客户名称不能为空" }]}
              >
                <Input placeholder="请输入客户名称" />
              </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item
                label="联系电话"
                name="tel"
                rules={[
                  { required: true },
                  { message: "联系人不能为空" },
                  {
                    pattern: /^1[3-9]\d{9}$/,
                    message: "请输入正确的11位手机号",
                  },
                ]}
              >
                <Input placeholder="请输入联系电话" />
              </Form.Item>
            </Col>
          </Row>
          <Row gutter={16}>
            <Col span={12}>
              <Form.Item
                label="经营状态"
                name="status"
                rules={[{ required: true }, { message: "请选择经营状态" }]}
              >
                <Radio.Group
                  name="radiogroup"
                  defaultValue={"1"}
                  options={[
                    { value: "1", label: "正常营业" },
                    { value: "2", label: "停业" },
                    { value: "3", label: "暂停营业" },
                  ]}
                />
              </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item
                label="所属行业"
                name="bussiness"
                rules={[{ required: true, message: "请选择所属行业" }]}
              >
                <Select
                  defaultValue="制造业"
                  options={[
                    { value: "制造业", label: "制造业" },
                    { value: "医疗", label: "医疗" },
                    { value: "新媒体", label: "新媒体" },
                    { value: "互联网", label: "互联网" },
                    { value: "新能源", label: "新能源" },
                    { value: "物流", label: "物流" },
                    { value: "电商", label: "电商" },
                  ]}
                />
              </Form.Item>
            </Col>
          </Row>
          <Row gutter={16}>
            <Col span={12}>
              <Form.Item
                label="邮箱"
                name="email"
                rules={[{ required: true, message: "邮箱不能为空" }]}
              >
                <Input />
              </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item
                label="统一信用代码"
                name="creditCode"
                rules={[{ required: true, message: "统一信用代码不能为空" }]}
              >
                <Input />
              </Form.Item>
            </Col>
          </Row>
          <Row gutter={16}>
            <Col span={12}>
              <Form.Item
                label="工商注册号"
                name="industryNum"
                rules={[{ required: true, message: "工商注册号不能为空" }]}
              >
                <Input />
              </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item
                label="组织机构代码"
                name="organizationCode"
                rules={[{ required: true, message: "组织机构代码不能为空" }]}
              >
                <Input />
              </Form.Item>
            </Col>
          </Row>
          <Row gutter={16}>
            <Col span={12}>
              <Form.Item
                label="法人名"
                name="legalPerson"
                rules={[{ required: true, message: "法人名不能为空" }]}
              >
                <Input />
              </Form.Item>
            </Col>
          </Row>
        </Form>
      </Modal>
    </>
  );
};

export default UserForm;
